<?php
namespace QnuiRingsMobile\Controller;
use Think\Controller;
class IndexController extends ExtendController {
	public function index(){
		$this->display();
	}
	public function get_page_js(){
		$page_id = I("page_id");
		$PISP = M("PageInshop");
		$js = $PISP->where(Array("id"=>$page_id))->getField("js");
		header("Content-type: text/javascript; charset=utf-8");
		echo base64_decode($js);
	}
	public function get_page(){
		$page_id = I("page_id");
		$sid = I("sid");
		$PG = M("PageInshop");
		
		// $ag = $_SERVER["HTTP_USER_AGENT"];
		// if(!strpos($ag,"taobao") && !strpos($ag,"tmall")){
			// //判断是否在支持的客户端打开
			// $page = current($PG->where(Array("id"=>$page_id))->getField("id,qrcode_url,short_url"));
			// $this->assign("qrcode",$page["qrcode_url"]);
			// $this->assign("url",$page["short_url"]);
			// $this->display("not_support");
			// die();
		// }
		
		$page = current($PG->where(Array("id"=>$page_id))->getField("id,status,js"));
		$this->assign("status",$page["status"]);
		$this->assign("settings",$page["js"]);
		$this->assign("page_id",$page_id);
		$this->display();
	}
	public function get_promo_items(){
		$taskId = I("taskId");
		$page = I("page");
		$pageSize = I("pageSize");
		$ITEMVIEW = D("TagTaskItemsView");
		$rule["tag_task_id"]=$taskId;
		$total = $ITEMVIEW->where($rule)->Count();
		if($total>0){
			$items = $ITEMVIEW->where($rule)->limit(($page-1)*$pageSize,$pageSize)->getField('id,pic_url,price,num_iid,name,promotion_type,promotion_value,is_limit_buy,limit_buy_count');
		}else{
			$items = Array();
		}
		foreach($items as $key=>$item){
			if($item["promotion_type"]==1){
				$items[$key]["final_price"] = ($item["price"]*$item["promotion_value"]/1000)/100;
			}else{
				$items[$key]["final_price"] = ($item["price"]-$item["promotion_value"])/100;
			}
			$items[$key]["price"]=$item["price"]/100;
		}
		
		$re = new \StdClass();
		$re->data=array_values($items);
		$re->total=$total;
		if(!$re->data && $total>0){
			$re->code = 910;
			$re->sub_msg = "读取数据出错";
		}
		$this->ajaxReturn($re);
	}
	public function check_nick(){
		$mixnick = I("mixnick");
		$countdown_id = I("countdown_id");
		$LOG = M("UserAddTagLog");
		$where = Array("task_id"=>$countdown_id,
						"buyer_nick"=>$mixnick
						);
		$tag_log = current($LOG->where($where)->getField("id,tag_action,add_time"));
		$re = new \StdClass();
		$re->data->log=$tag_log;
		$this->ajaxReturn($re);
	}
	public function start_task(){
		$openuid = I("openuid");
		$nick = I("nick");
		$mixnick = I("mixnick");
		$countdown_id = I("countdown_id");
		$LOG = M("UserAddTagLog");
		$where = Array("task_id"=>$countdown_id,
						"buyer_nick"=>$mixnick
						);
		$log_id = $LOG->where($where)->getField("id");
		$re = new \StdClass();
		$re->data->log_id=$log_id;
		if(!$log_id){
			//第一步 读取任务信息
			$TSK = D("TagTaskView");
			$task = current($TSK->where(Array("tag_task_id"=>$countdown_id))
							    ->getField("sid,tag_id,tag_name,start_time,end_time,countdown,is_vip,vip_group,user_nick,sessionkey"));
			if(strtotime($task["start_time"])>time()){
				$re->code = 932;
				$re->msg = "活动尚未开始，请耐心等待！";
				$this->ajaxReturn($re);
				die();
			}
			if(strtotime($task["end_time"])<time()){
				$re->code = 933;
				$re->msg = "抱歉，活动已结束，请您关注我们更多的活动！";
				$this->ajaxReturn($re);
				die();
			}
			if($task["is_vip"]){
				$group = $task["vip_group"];
			}else{
				$group = "defaultService";
			}
			//第二步 打标
			Vendor('taobaosdk/top/request/TmallPromotagTaguserSaveRequest');
			$c = new \TopClient;
			$c->appkey = C('WEIZHE_APPKEY');
			$c->secretKey = C('WEIZHE_APP_SECRET');
			$req = new \TmallPromotagTaguserSaveRequest;
			$req->setTagId($task["tag_id"]);
			$req->setNick($mixnick);
			$req->putOtherTextParam("top_mix_params", "nick");//就会进行解混淆
			$resp = $c->execute($req, $task["sessionkey"]);
			if(!$resp->is_success){
				$re->code = $resp->code;
				$re->msg = $resp->msg.$resp->sub_msg;
				$this->ajaxReturn($re);
				die();
			}
			//第三步 加入日志
			$log_data["sid"]=$task["sid"];
			$log_data["tag_id"]=$task["tag_id"];
			$log_data["tag_name"]=$task["tag_name"];
			$log_data["tag_action"]="add";
			$log_data["add_time"]=date("Y-m-d H:i:s");
			$log_data["countdown"]=$task["countdown"];
			$log_data["buyer_nick"]=$mixnick;
			$log_data["seller_nick"]=$task["user_nick"];
			$log_data["oid"]=1;
			$log_data["task_id"]=$countdown_id;
			$add_log_id = $LOG->add($log_data);
			if(!$add_log_id){
				$re->code = "910";
				$re->msg = "数据库错误！";
				$this->ajaxReturn($re);
				die();
			}
			try{
				$con = new \Stomp(C("ActiveMQ"),C("ActiveMQUser"),C("ActiveMQPass"));
				$con_re = $con->send($group, json_encode(Array("add_log_id"=>$add_log_id,"sid"=>$task["sid"],"tag_id"=>$task["tag_id"],"buyer_nick"=>$mixnick,"is_mixnick"=>true)),array("amq-msg-type" => "text","persistent"=>"true","AMQ_SCHEDULED_DELAY"=>($task["countdown"]*60*1000)));
			} catch (\StompException $e) {
				$LOG->where(Array("id"=>$add_log_id))->delete();
				$re->code = "916";
				$re->msg = "连接消息服务失败！".$e->getMessage();
				$this->ajaxReturn($re);
				die();
			}
			
		}
		$re->code = 200;
		$this->ajaxReturn($re);
	}
}